﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Google.GData;
using Google.Contacts;
using Google.GData.Contacts;
using Google.GData.Client;
using Google.GData.Extensions;
using System.Threading;

namespace GmailContactsNormalizer
{
    public partial class Form1 : Form
    {

        string captchaToken;
        RequestSettings rs;// = new RequestSettings("GmailContactsNormalizer", "liz@gmail.com", "password");        
        ContactsRequest cr;// = new ContactsRequest(rs);
        IEnumerable<Contact> contacts;

        public Form1()
        {
            InitializeComponent();            
            rs = new RequestSettings("GmailContactsNormalizer", textBox1.Text, textBox2.Text);
            rs.AutoPaging = true;
            cr = new ContactsRequest(rs);
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Thread th = new Thread(new ThreadStart(login));
            th.Start();
        }

        private void login()
        {
            button1.Enabled = false;
            try
            {
                if (comboBox1.SelectedItem == null)
                {
                    Feed<Group> gs = cr.GetGroups();
                    comboBox1.Items.Clear();
                    comboBox1.DisplayMember = "Title";
                    foreach (Group g in gs.Entries)
                    {
                        comboBox1.Items.Add(g);
                    }
                    comboBox1.SelectedIndex = 0;
                    comboBox2.SelectedIndex = 0;
                }
                else
                {
                    Group g = comboBox1.SelectedItem as Group;
                    ContactsQuery q = new ContactsQuery(ContactsQuery.CreateContactsUri("default"));
                    q.Group = g.Id;
                    Feed<Contact> f = cr.Get<Contact>(q);
                    this.contacts = f.Entries;
                    List<string> display = new List<string>();
                    foreach (Contact entry in this.contacts)
                    {
                        if (entry.Name != null)
                        {
                            Name name = entry.Name;
                            string cn = "";
                            string dn = "";
                            string rsho = "";
                            if (Chinese2Spell.isChinese(name.GivenName) && Chinese2Spell.isPinYinFor(name.FamilyName, name.GivenName))
                            {
                                cn = name.GivenName;
                                dn = name.GivenName;
                                rsho = name.FamilyName;
                            }
                            else
                            {
                                if (name.FamilyName != null && name.FamilyName.Length > 0)
                                {
                                    cn += name.FamilyName;
                                    dn += name.FamilyName + ", ";
                                }
                                if (name.GivenName != null && name.GivenName.Length > 0)
                                {
                                    cn += name.GivenName;
                                    dn += name.GivenName;
                                }
                            }
                            if (Chinese2Spell.isChinese(cn))
                            {
                                string sho;
                                switch (comboBox2.SelectedIndex)
                                {
                                    case 0:
                                        sho = Chinese2Spell.Convert(cn);
                                        break;
                                    case 1:
                                        sho = Chinese2Spell.GetSpellStringLower(cn);
                                        break;
                                    case 2:
                                        sho = Chinese2Spell.GetSpellStringLower(cn).ToUpper();
                                        break;
                                    default:
                                        display.Add("错误的格式选项");
                                        textBox3.Lines = display.ToArray();
                                        return;
                                }
                                if (rsho != sho)
                                {
                                    display.Add(dn + " -> " + sho + ", " + cn);
                                    name.GivenName = cn;
                                    name.FamilyName = sho;                                    
                                    cr.Update<Contact>(entry);
                                    textBox3.Lines = display.ToArray();
                                    textBox3.SelectionStart = textBox3.Text.Length;
                                    textBox3.ScrollToCaret();
                                }
                                else
                                {
                                    display.Add("Skip: " + dn);
                                    textBox3.Lines = display.ToArray();
                                    textBox3.SelectionStart = textBox3.Text.Length;
                                    textBox3.ScrollToCaret();  
                                }
                            }
                            else
                            {
                                display.Add("Skip: " + dn);
                                textBox3.Lines = display.ToArray();
                                textBox3.SelectionStart = textBox3.Text.Length;
                                textBox3.ScrollToCaret();                                
                            }
                        }
                    }
                }
            }
            catch (CaptchaRequiredException ex)
            {
                
                this.captchaToken = ex.Token;
               
            }
            button1.Enabled = true;
        }
        
        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            rs.Credentials.CaptchaAnswer = textBox3.Text;
            rs.Credentials.CaptchaToken = captchaToken;
            login();
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
